package com.zrrd.learn.framework.mybatis.javabase.util;

import com.zrrd.learn.framework.mybatis.mybatisinterface.mapper.StudentMapper;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;


import javax.sql.DataSource;

/**
 *
 * 通用的功能设计封装
 * <p>
 * 对mybatis通用部分进行面向对象的封装
 * @author wrn
 * @version 1.0
 * @date 2023/9/4 10:30
 */

public class MybatisUtil {

    private final String url = "jdbc:mysql://localhost:3309/exercise01?timeZone=Asia/Harbin&characterEncoding=utf-8";
    private final String user = "root";
    private final String password = "Wrn147852!";

    /**
     * 想使用mybatis 一定要有四大基础信息
     * 1 驱动类
     * 2 mybatis 包
     * 3 : mysql 四大基础信息
     * 3.1 乱码  时区的问题
     * 4 : 想办法  3--> sqlSessionFactoryBuilder -->  sqlSessionFactory -->   sqlSession  --> getMapper(） --> 接口代理 ->> 调用里面的方法
     */

    /**
     * 用来生成数据源
     *
     * @param url      连接串
     *                 ex jdbc:mysql://localhost:3309/exercise01?timeZone=Asia/Harbin&characterEncoding=utf-8
     * @param user     用户名
     * @param password 密码
     * @return
     */

    private DataSource createDataSource(String url,String user,String password) {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUser(this.user);
        dataSource.setPassword(this.password);
        dataSource.setURL(this.url);
        return dataSource;
    }

    private DataSource createDataSource() {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUser(this.user);
        dataSource.setPassword(this.password);
        dataSource.setURL(this.url);
        return dataSource;
    }

    /**
     * 为我们产生了一个环境类吧
     * @return
     */
    private Environment initEnv() {
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment e = new Environment("wrnlocalmysql",transactionFactory,this.createDataSource());
        return e;
    }

    private Configuration initConfiguration() {
        Configuration configuration = new Configuration();

        //setting
        configuration.setCacheEnabled(true);
        //打开sql打印功能  开发阶段
        configuration.setLogImpl(StdOutImpl.class);

        //把环境给配置
        configuration.setEnvironment(this.initEnv());

        //configuration添加映射器
        configuration.addMappers("com.zrrd.learn.framework.mybatis.javabase.mapper");

        return configuration;
    }

    public SqlSession getSqlSession(){

        //工厂建造的实例化
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        // 实例化工厂
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(this.initConfiguration());

        return sqlSessionFactory.openSession();
    }

    public void commit(SqlSession session){
        session.commit();
    }

    public void rollback(SqlSession session){
        session.rollback();
    }

    public void close(SqlSession session){
        session.close();
    }

}
